package com.desin.modle.multithread;

import java.util.concurrent.atomic.AtomicReference;

/**
 *
 do {
 // 获取当前值
 oldV = xxxx；
 // 根据当前值计算新值
 newV = ...oldV...
 }while(!compareAndSet(oldV,newV);
 */
public class CuAtomicReference {

        class WMRange{
            final int upper;
            final int lower;
            WMRange(int upper,int lower){
                //省略构造函数实现
                this.upper = upper;
                this.lower = lower;
            }
        }
        final AtomicReference<WMRange>
                rf = new AtomicReference<>(
                new WMRange(0,0)
        );
        // 设置库存上限
        void setUpper(int v){
            WMRange nr;
            WMRange or;
            do{
                or = rf.get();
                // 检查参数合法性
                if(v < or.lower){
                    throw new IllegalArgumentException();
                }
                nr = new
                        WMRange(v, or.lower);
            }while(!rf.compareAndSet(or, nr));
        }

}
